local lsp_xiangong = fk.CreateSkill {
    name = "lsp_xiangong",
}

Fk:loadTranslationTable {
    ["lsp_xiangong"] = "先攻",
    [":lsp_xiangong"] = "当你成为其他角色使用牌的目标后，若通过〖恃缪〗移出游戏的牌中有针对其的牌，你可以对其依次使用这些牌并移去使用的牌，若其受到这些牌造成的伤害，其使用的牌对你无效。",

}

lsp_xiangong:addEffect(fk.TargetConfirming, {
    anim_type = "support",
    can_trigger = function(self, event, target, player, data)
        local mark = player:getTableMark("lsp_shimou")
        return target == player and player:hasSkill(lsp_xiangong.name) and
            data.from ~= player and table.contains(mark, data.from.id)
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player,
            { prompt = "先攻：是否反制其使用的牌，使用者" .. data.from.id, skill_name = lsp_xiangong.name })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cards = {}
        for _, id in ipairs(player:getPile("#shimou_po")) do
            if Fk:getCardById(id):getMark("lsp_shimou_id") == data.from.id then
                table.insertIfNeed(cards, id)
            end
        end
        while #cards > 0 and not player.dead and not data.from.dead do
            if data.from.dead then break end
            local id = room:askToCards(player, {
                min_num = 1,
                max_num = 1,
                pattern = tostring(Exppattern { id = cards }),
                prompt = "先攻：请选择要使用的牌，目标角色" .. data.from.id,
                skill_name = "lsp_xiangong",
                cancelable = false,
                expand_pile = cards,
            })[1]
            local card = Fk:getCardById(id)
            if player:canUseTo(card, data.from, { bypass_distances = true, bypass_times = true }) then
                local use = {
                    from = player,
                    tos = { data.from },
                    card = card,
                    extra_data = {
                        lsp_xiangong_use = player,
                    }
                }
                room:useCard(use)
                if use.damageDealt and player:getMark("lsp_xiangong_nb") == 0 then
                    room:notifySkillInvoked(player, lsp_xiangong.name, "defensive")
                    data.use.nullifiedTargets = data.use.nullifiedTargets or {}
                    table.insertIfNeed(data.use.nullifiedTargets, player)
                    room:setPlayerMark(player, "lsp_xiangong_nb", 1)
                end
                table.removeOne(cards, id)
            else
                room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, lsp_xiangong.name, nil, true,
                    player)
            end
        end
        room:removeTableMark(player, "lsp_shimou", data.from.id)
        room:setPlayerMark(player, "lsp_xiangong_nb", 0)
    end,
})

return lsp_xiangong
